package com.cet.algorithm.字符串;

/**
 * @program: algorithm
 * @description: 字符串空格替换
 * @author: 陈恩涛
 * @create: 2022-03-19 11:24
 **/
public class JZOffer05 {

    public static String replaceSpace(String s) {
        int slowIndex = 0;
        StringBuilder result = new StringBuilder();
        for (int fastIndex = 0; fastIndex < s.length(); fastIndex++) {
            if (s.charAt(fastIndex) == ' ') {
                result.append(s.substring(slowIndex, fastIndex));
                result.append("%20");
                slowIndex = fastIndex + 1;
            }
        }
        if (slowIndex < s.length()) {
            result.append(s.substring(slowIndex));
        }
        return result.toString();
    }
}
